package com.example.utils;

import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

import java.io.FileInputStream;

public class WordUtils {
    public static String readWordUtils(String path){
        FileInputStream is=null;
        OPCPackage opcPackage=null;
        try {
            if (path.endsWith(".doc")) {
                is = new FileInputStream(path);
                //    WordExtractor ex = new WordExtractor(is);
                //  return ex.getText();
            }
            if (path.endsWith(".docx")) {
                opcPackage= POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                return extractor.getText();
            }
            return "此文件不是word文件！";
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(is!=null){
                    is.close();
                }
                if(opcPackage!=null){
                    opcPackage.close();
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return "";
    }

}
